import  File from './basics/File.js'
import  Block from './basics/Block.js'
import Pack from "./basics/Pack";
import Word from "./word/Word.js";
import Row from "./basics/Row.js";
import StrTo from "../lex/StrTo";
import  RunOrderFile from "./run/RunOrderFile.js"
/**
 * 中心
 */
export  default  class Core{

    constructor(){
        this.process=new File();
        this.process.addData(0,null,null,new Block() );
        this.pack=new Pack();
        this.stack={};
        this.heap=new Pack();
        this.operation=[];
        this.word=new Word(this)
    }

    static   instance() {
        if (!Core._instance) {
            Object.defineProperty(Core, "_instance", {
                value: new Core()
            });
        }
        return Core._instance;
    }


    /**
     *  参数都是 buff
     * @param dataRow
     * @param dataBlock
     * @param linkRow
     */
    loadData(data ){
        this.pack.loadData(data)
        //导入之后，需要判断一下，1里面有没有数据，如果没有添加一下
        /*    let  square=   this.pack.findData(1,0,null,null);
            if(!square){
                this.pack.addData(1,null,null,new Block()) // 虚 块 添加 square
            }*/
    }

    outData(){
        let buff=this.pack.outData();
        return  buff;
    }
    /**
     * 初始化
     */
    init(){
        this.pack=new Pack();
        // this.addFile(); //目录 可能也不需要、 、
        // 字初始化
        this.pack.addData(null,null,null,new File()) //实  块
        this.pack.addData(null,null,null,new File()) //虚  块
        this.pack.addData(1,null,null,new Block()) // 虚 块 添加 square
        // 添加文件可以单独分析
        //  this.addFile();// 意思 块
        //程序不需要初始化，直接添加文件
        //this.addFile();// 程序

    }


    /**
     * 添加任务
     */
    /**
     *
     *
     *  如果任务切换，做一个列表进行查询，然后把任务合并进去
     *  现在，只做一个任务的的流程
     *
     * @param pid
     * @param para
     * @param source
     * @param taskType  A 设置环境 B 内容
     */
    addTask( para, taskType ){
        if("A"==taskType){
            this.setOutType="A"
        }else {
            this.setOutType="B"

        }
        let _this=this;
        let file=null;
        let word =new Word(this);
        let wordIds= _this.words(para);
        let xx=  word.ideaRun(wordIds);
        file=  word.analysisRun(xx);


    }
    /**
     * 字
     * @param strNums
     */
      words(str){
        //拆分直接添加
        let res=[];
        let srts=str.split("");
        for(let i=0;i<srts.length;i++){
            let num= StrTo.utf8ToNum(srts[i]);
            let wordId = this.word.addWord(num);
            res.push(wordId)
        }
        return res;

    }

    /**
     * operation
     */
    addOperation(tesk){
        this.operation.push(tesk);
    }

    async runss(){
        while (true){
            await   run();
        }
    }

     run(){
        let _this=this;
      return new Promise(resolve => {setTimeout(function(){_this._run(_this.operation.length),resolve(1)},1000)} )  ;
    }

    _run(i){
        if(i>=this.operation.length){
            i=this.operation.length-1;
        }else {
            i--;
        }
        if(i >=0){
            this.operation[i].run()
        }else {
            i=this.operation.length-1;
        }

    }


    /**
     *通常是链接
     */
    addEmpty(row,emptyId){
        if(emptyId){
            return  this.pack.addData(1,0,emptyId,row)
        }
        return  this.pack.addData(1,0,null,row)
    }

    /**
     *
     * @param row
     * @param func
     * @param resType  Default empty  id
     * @returns {*}
     */
    findEmptyId(row,func,resType){
        if(resType){
            this.resEmptyType=resType;
        }else {
            this.resEmptyType='empty';
        }
        if(row){
            this.emptyRow=row;
        }else {
            this.emptyRow=new Row(null,null,null,null);
        }
        if(func){
            return this.pack.findFileData(1,0,0,func)
        }
        return this.pack.findFileData(1,0,0,this.empthDefaultFunc.bind(this))
    }
    empthDefaultFunc(FileId,BlockId,RowId,dataThis){
        let word=this.emptyRow;

        for(let n=0;n<dataThis.list.length;n++){
            let row=  dataThis.list[n];
            let res=[];
            if(word.find(1)===null||row.isValue(1,word.find(1)) ) {
                res.push(1)
            }
            if(word.find(2)===null||row.isValue(2,word.find(2))  ){
                res.push(1)
            }
            if(word.find(3)===null||row.isValue(3,word.find(3)) ){
                res.push(1)
            }
            if(word.find(4)===null||row.isValue(4,word.find(4))  ){
                res.push(1)
            }
            if(res.length==4){
                if(this.resEmptyType=='empty') {
                    return row;
                }
                return n;
            }
        }
        return null;

    }
    /**
     * 添加一个实体
     */
    addReal(row,realId){
        if(!realId){
            realId=  this.pack.addData(0,null,null,new Block());
        }
        this.pack.addData(0,realId,null , row);
        return realId;
    }

    delIdea(fileId,realId,rowId,row){
        this.pack.addData(0,realId,rowId , row);
    }

    /**
     * 修改实体
     * @param real
     * @param realId
     * @returns {*}
     */
    upReal(real,realId){
        if(!realId){
          return  this.pack.addData(0,null,null,real);
        }
        this.upRealData=real;
        this.blockId=realId;
        this.pack.findData(0,null,null,this.upRealFunc.bind(this))
        return realId;
    }
    upRealFunc(FileId,BlockId,RowId,dataThis){
        dataThis.list[this.blockId]=this.upRealData;

    }

    /**
     *
     * @param row
     * @param func
     * @param resType
     * @returns {*}
     */
    findReal(row,realId,func,resType){
        if(realId!=null&&typeof(realId) != "undefined"){
            this.realId=realId;
            return this.pack.findData(0,null,null,this.realDefaultFunc.bind(this))
        }
        if(resType){
            this.resRealType=resType;
        }else {
            this.resRealType='real';
        }
        if(row){
            this.realRow=row;
        }else {
            this.realRow=new Row(null,null,null,null);
        }
        if(func){
            return this.pack.findFileData(1,0,0,func)
        }
        return this.pack.findData(0,null,null,this.realDefaultFunc.bind(this))
    }
    realByIdFunc(FileId,BlockId,RowId,dataThis){
        let   realId=this.realId;
        return dataThis.list[realId];
    }

    realDefaultFunc(FileId,BlockId,RowId,dataThis){
        let word=this.realRow;
        let realId= this.realId;
        if(realId!=null){
            this.realId=null;
            return  dataThis.list[realId];
        }
        for(let n=0;n<dataThis.list.length;n++){
            let square=  dataThis.list[n];
            for(let l=0;l<square.list.length;l++){
                let row=square.list[l];
                let res=[];
                if(row.isValue(1,word.find(1))){
                    res.push(1)
                }
                if(row.isValue(2,word.find(2))){
                    res.push(2)
                }
                if(row.isValue(3,word.find(3)) ){
                    res.push(3)
                }
                if(row.isValue(4,word.find(4))){
                    res.push(4)
                }
                if(res.length==4){
                    if(this.resRealType=='real') {
                        return square;
                    }
                    return l;
                }
            }
        }
        return null;

    }

}

class Sentence{

    static  run(str){
        outMes("A","xxx","xxx");
        let str1= str.slice(0,str.indexOf('\n'))
        let res="";
        if(str1 =='字'){
            res= this. word(str.slice(str.indexOf('\n')+1));
        }
        if(str1 =='词'){
            let swordIds= this. word(str.slice(str.indexOf('\n')+1));
            res=this.words(swordIds)
        }
        if(str1=='虚'){
            let real2=str.split('\n');
            let real3=real2[1].split(',' );

            let row= trans.findEmptyId(new Row(...real3))
            if(!row){
                row=   trans.findEmptyId(new Row(...real3),null,'1')
            }
            if(!row){
                let reodId= trans.addReal();
                let words1= new Row(...real3)
                words1.save(3,reodId)
                trans.addEmpty(words1);
                row=words1;
            }
            res=row;
        }
        if(str1 =='意'){

            let real2=str.split('\n');
            let real3=real2[1].split(',' );
            if(real2.length==3){
                let real4=real2[2].split(',' );
                if(real3.length==2){
                    trans.delIdea(0,real3[0],real3[1], new Row (...real4))
                }else {
                    let row =new Row (...real4)
                    trans.addReal(row,real3[0])
                }
            }

            let real= trans.findReal(null,real3[0],null,null)

            for(let i=0;i<real.list.length;i++){
                res+=real.list[i].toString()+"\n";
            }



        }
        return res.toString();
    }

    empthDefaultFunc(FileId,SquareId,RowId,dataThis){
        let word=this.emptyRow;
        debugger
        dataThis.list[this.id]=word

        return null;

    }
    run1(str){
        let real2=str.split('\n');
        let real3;



        if(real2.length==2){
            real3=real2[1].split(',' );
            let real1= real2[0].split(',' );

            if(real1[0]=='改'){
                this.id=real1[1];
                this.emptyRow= new Row(...real3)
                let row= trans.findEmptyId(new Row(...real3),this.empthDefaultFunc.bind(this) )

                return new Row(...real3).toString();
            }



            let row= trans.findEmptyId(new Row(...real3),null,2)
            if(!row){
                row=   trans.findEmptyId(new Row(...real3),null,'1')
            }
            if(!row){
                let words2= new Row(...real3)
                let reodId= trans.addReal(words2);
                let words1= new Row(...real3)
                words1.save(3,reodId)
                trans.addEmpty(words1);
                row=words1;
            }
            return row.toString();
        }



        real3=str.split(',' );


        let row= trans.findEmptyId(new Row(...real3))
        if(!row){
            row=   trans.findEmptyId(new Row(...real3),null,'1')
        }
        if(!row){
            let words2= new Row(...real3)
            let reodId= trans.addReal(words2);
            let words1= new Row(...real3)
            words1.save(3,reodId)
            trans.addEmpty(words1);
            row=words1;
        }
        return row.toString();
    }
    analysis(){

    }


    /**
     * 字
     * @param strNums
     */
    static word(str){
        //拆分直接添加
        let res=[];
        let srts=str.split("");
        for(let i=0;i<srts.length;i++){
            let num= StrTo.utf8ToNum(srts[i]);
            let wordId =trans.addWord(num);
            res.push(wordId)
        }
        return res;

    }

    /**
     * 通过字或者词添加意
     * 意
     */
    idea(){
        //判断是字还是词
        //    如果是词查找是否存在，不存在创建
        //查找意是否存在，不存在创建

    }

    /**
     * 组词
     */
    static words(wordIds){
        return  trans.addWrods(wordIds[0],wordIds[1])
        //查找每个字是否存在
        //组词 某认情况下不创建，除非添加意
    }



}